var request = require('../../utils/request.js')
var app = getApp();
Page({
  data: {
    showd: false,
    _token:"",
    list:[],
    postList:{},
    sign:true,
    pocketlist:"",
    mobile:""
  },

  //  充值协议弹出框
  activity_click() {
    var that = this;
    that.setData({
      showd: !that.data.showd
    })
  },
  onLoad: function () {
    this.getTop_up_fees()
  },
  //获取充值套餐
  getTop_up_fees(){
    let success = (res) => {
      this.data.list=res.data
      for (let i = 0; i < this.data.list.length;i++){
        // this.data.list[i].cash = parseInt(this.data.list[i].cash)
        this.data.list[i].present_cash = parseInt(this.data.list[i].present_cash)
      }
      this.setData({
        list: this.data.list
      })
    }
    request.getdata("/pocket/top_up_fees", {area_id: '510107'}, success, this);
  },
  // 充值
  top_up: function(e){
    if(this.data.mobile){
      this.data.postList = e.currentTarget.dataset.item;
      this.data.postList.source =1;
      this.data.postList.balance =this.data.pocketlist.balance;
      this.data.postList.fee_id = this.data.postList.id;
       if (this.data.sign==true){
        this.pay()
        this.data.sign = false;
        setTimeout(() => {
          this.data.sign = true
        }, 10000)
      }else{
        return;
      }
    }else{
      my.confirm({
        title: '温馨提示',
        content: '还没有绑定手机哦!',
        confirmButtonText: '马上绑定',
        cancelButtonText: '暂不需要',
        success: (result) => {
          if(result.confirm){
            my.navigateTo({
              url: '/pages/user/mobile'
            })
          }else if (result.cancel) {
            // console.log('用户点击取消')
          }
          
        },
      });
    }
    
  },
  onShow: function () {
    my.getNetworkType({
      success: (res) => {
        if(!res.networkAvailable||res.networkType=='UNKNOWN'){
          my.showToast({
            content: '网络状态不佳，请检查网络',
            type:'fail',
            duration: 2000
          })
        }
      }
    })
    this.getuserinfo();
  },
  pay:function(){
     this.data.postList.area_id=this.data.area_id
      let success=(param)=>{
        if(param == "nomoney"){
          my.redirectTo({
            url: '/pages/user/wallet'
          });
        }else{
          my.tradePay({
          orderStr: param.callback, 
          success: (res) => {           
            if (res.resultCode==9000) {
              my.redirectTo({
                url:'/pages/user/wallet'
              })
              my.showToast({
                content:'支付成功',
                type:'success',
                duration: 1000
              })
            }
          },
          fail: (res) => {
          }
          });
        }
        
      }
    request.postdata('/pocket/top_up',this.data.postList,success,this)
  },
  //获取页面数据
  getuserinfo() {
    let success = (res) => {
      if(res.telephone){
        this.data.mobile = res.telephone;
      }
      this.setData({
        pocketlist: res.pocket
      })
    }
    request.getdata("/users/user/show", {}, success, this);
  }
})